package in.srain.cube.views.ptr.demo.ui.classic;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import in.srain.cube.mints.base.TitleBaseFragment;
import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;
import in.srain.cube.views.ptr.demo.R;
import in.srain.cube.views.ptr.loadmore.OnLoadMoreListener;

public class WithListView extends TitleBaseFragment {

    private MyAdapter mAdapter;
    private PtrClassicFrameLayout mPtrFrame;
    private List<String> mData = new ArrayList<>();
    TextView no_data;

    @Override
    public View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        setHeaderTitle(R.string.ptr_demo_block_list_view);


        final View contentView = inflater.inflate(R.layout.fragment_classic_header_with_list_view, null);
        final ListView listView = (ListView) contentView.findViewById(R.id.rotate_header_list_view);
        no_data = (TextView) contentView.findViewById(R.id.no_data);
        //        mPtrFrame.setLoadMoreEnable(true);

        //        mPtrFrame.removeView(listView);
        //        mPtrFrame.addView(no_data);

        mAdapter = new MyAdapter();
        listView.setAdapter(mAdapter);


        mPtrFrame = (PtrClassicFrameLayout) contentView.findViewById(R.id.rotate_header_list_view_frame);
        mPtrFrame.setEnabledNextPtrAtOnce(true);
        mPtrFrame.setLastUpdateTimeRelateObject(this);
        mPtrFrame.setPtrHandler(new PtrHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                //                frame.removeView(no_data);
                //                frame.addView(listView);
                no_data.setVisibility(View.GONE);
                listView.setVisibility(View.VISIBLE);

                mData.clear();
                for (int i = 0; i < 5; i++) {
                    mData.add("refresh " + i);
                }
                mPtrFrame.refreshComplete();
                mPtrFrame.loadMoreComplete(mAdapter.getCount() > 10);
                mAdapter.notifyDataSetChanged();
            }

            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return PtrDefaultHandler.checkContentCanBePulledDown(frame, content, header);
            }
        });

        mPtrFrame.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void loadMore() {
                mPtrFrame.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        for (int i = 0; i < 5; i++) {
                            mData.add("loadmore " + i);
                        }

                        mPtrFrame.loadMoreComplete(true);
                        mAdapter.notifyDataSetChanged();
                    }
                }, 0);
            }
        });
        // the following are default settings
        mPtrFrame.setResistance(1.7f);
        mPtrFrame.setRatioOfHeaderHeightToRefresh(1.2f);
        mPtrFrame.setDurationToClose(200);
        mPtrFrame.setDurationToCloseHeader(1000);
        // default is false
        mPtrFrame.setPullToRefresh(false);
        // default is true
        mPtrFrame.setKeepHeaderWhenRefresh(true);
        mPtrFrame.postDelayed(new Runnable() {
            @Override
            public void run() {
                mPtrFrame.autoRefresh();
            }
        }, 100);
        return contentView;
    }

    private class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return mData.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = View.inflate(getContext(), android.R.layout.simple_list_item_1, null);
            }
            TextView textView = (TextView) convertView;
            textView.setText(mData.get(position));
            return convertView;
        }
    }
}